package com.linsir.lin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linsir.lin.mapper.AccountRoleMapper;
import com.linsir.lin.pojo.Account;
import com.linsir.lin.pojo.AccountRole;
import com.linsir.lin.service.AccountRoleService;
import lombok.extern.slf4j.Slf4j;
import org.camunda.feel.syntaxtree.In;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class AccountRoleServiceImpl extends ServiceImpl<AccountRoleMapper, AccountRole> implements IService<AccountRole>, AccountRoleService {
    /**
     * 根据账号Id，账号角色的关系列表
     *
     * @param accountId
     * @return
     */
    @Override
    public List<AccountRole> accountRoleList(Integer accountId) {
         QueryWrapper<AccountRole> queryWrapper =new QueryWrapper<AccountRole>();
         queryWrapper.eq("account_id",accountId);
         List<AccountRole>  accountRoleList=list(queryWrapper);
         return accountRoleList;
    }

    /**
     * 根据角色id和账号id找到器对用关系对象
     *
     * @param accountId
     * @param roleId
     * @return
     */
    @Override
    public AccountRole getbyAccountIdAndRoleId(Integer accountId, Integer roleId) {
        QueryWrapper<AccountRole> queryWrapper =new QueryWrapper<AccountRole>();
        queryWrapper.eq("account_id",accountId).eq("role_id",roleId);
        return  getOne(queryWrapper);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Override
    public boolean batchDel(List<Integer> ids) {
        log.info(String.valueOf(removeByIds(ids)));
        return removeByIds(ids);
    }

    @Override
    public boolean batchDelByAccount(Integer accountId) {
        List<Integer> ids = new  ArrayList<Integer>();
        accountRoleList(accountId).
                stream().
                forEach( a -> ids.add(a.getId()));
        return batchDel(ids);
    }

    /**
     * 批量存储
     *
     * @param accountRoles
     * @return
     */
    @Override
    public boolean batchSave(List<AccountRole> accountRoles) {
        return saveBatch(accountRoles);
    }

    @Override
    public boolean batchSaveByMap(Map<String, Object> map,Integer accountId) {
        List<AccountRole> accountRoles =  new  ArrayList<AccountRole> ();

        map.entrySet().forEach(a->{
            AccountRole accountRole = new AccountRole();
            accountRole.setAccountId(accountId);
            accountRole.setRoleId(Integer.parseInt(a.getValue().toString()));
            accountRoles.add(accountRole);
        });

        return batchSave(accountRoles);
    }


}
